##################################################################################################
# Replication files for: Collaboration and reciprocity in the Texas State House of Representatives
# Authors: Emma Crewe and Michelle Taylor-Robinson
# USing R 4.4.2
##################################################################################################

# Clear environment and load data.
rm(list = ls()) 
# Set working directory to file source.
JointAuthorNetwork <- readxl::read_excel("JointAuthorNetwork.xlsx")
CoauthorNetwork <- readxl::read_excel("CoauthorNetwork.xlsx")

RepAttributes <- readxl::read_excel("(Aug14)CLEANRepAttributes.xlsx")
RepAttributes <- RepAttributes[1:148,]

###############
## Network construction. 
###############
#Using the "igraph" package to coerce the imported data into network objects. 
library(igraph)
#Note that this package specifies the direction of connection from row to column. 

#joint author network first
#joint author network is directed from column to row, so we need to transpose the matrix first. 
#the following command transposes the entries of the joint author network minus the first column, which contains the names of the representatives. 
Tjointauthor <- as.matrix(t(JointAuthorNetwork[,-1]))
names <- as.list(JointAuthorNetwork[,1])
colnames(Tjointauthor) <- names$RepName
JA.net <- graph_from_adjacency_matrix(Tjointauthor,
                                      mode= c("directed"),
                                      weighted = NULL, 
                                      diag = FALSE, add.colnames = NA)

#coauthor network next
#we need to turn these into matrices and also remove the first column of repnames. 
CoauthorNetwork <- as.matrix(CoauthorNetwork[,-1])
CA.net <- graph_from_adjacency_matrix(CoauthorNetwork,
                                      mode= c("directed"),
                                      weighted = NULL, 
                                      diag = FALSE, add.colnames = NA)


RepAttributes$Race[41] <- 1 #First recode Garcia(41) to be hispanic rather than both hispanic and black or "1 and 2."

#note that there are two ways to incorporate the data within the matrix that specify ties. 
#I specify the argument "weighted" as "TRUE", which will generate a network object with ties that are weighted by the numeric values contained in the matrix. 
#you can also specify weighted as "NULL", which will generate x number of ties corresponding to the value inside the matrix. 

# supplying the joint author network with all the rep attributes. 
V(JA.net)$RepName <- RepAttributes$RepName
V(JA.net)$LastName <- RepAttributes$`Last name`
V(JA.net)$FirstName <- RepAttributes$`First name(s)`
V(JA.net)$TotalBillsAuthored <- RepAttributes$TotalBillsAuthored
V(JA.net)$Woman <- RepAttributes$Woman
#V(JA.net)$RaceEthnicity <- RepAttributes$RaceEthnicity
V(JA.net)$Race <- RepAttributes$Race
V(JA.net)$white <- RepAttributes$white
V(JA.net)$Hispanic <- RepAttributes$Hispanic
V(JA.net)$Black <- RepAttributes$Black
V(JA.net)$Asian <- RepAttributes$Asian
V(JA.net)$Party <- RepAttributes$Party
V(JA.net)$Democrat <- RepAttributes$Democrat
V(JA.net)$FreedomCaucus <- RepAttributes$FreedomCaucus
V(JA.net)$WaterCaucus <- RepAttributes$WaterCaucus
V(JA.net)$EnergyClimateCaucus <- RepAttributes$EnergyClimateCaucus
V(JA.net)$ITCaucus <- RepAttributes$ITCaucus
V(JA.net)$ChildCaucus <- RepAttributes$ChildCaucus
V(JA.net)$TermSeniority <- RepAttributes$TermSeniority


# and the same for the coauthor network.
V(CA.net)$RepName <- RepAttributes$RepName
V(CA.net)$LastName <- RepAttributes$`Last name`
V(CA.net)$FirstName <- RepAttributes$`First name(s)`
V(CA.net)$TotalBillsAuthored <- RepAttributes$TotalBillsAuthored
V(CA.net)$Woman <- RepAttributes$Woman
#V(CA.net)$RaceEthnicity <- RepAttributes$RaceEthnicity
V(CA.net)$Race <- RepAttributes$Race
V(CA.net)$white <- RepAttributes$white
V(CA.net)$Hispanic <- RepAttributes$Hispanic
V(CA.net)$Black <- RepAttributes$Black
V(CA.net)$Asian <- RepAttributes$Asian
V(CA.net)$Party <- RepAttributes$Party
V(CA.net)$Democrat <- RepAttributes$Democrat
V(CA.net)$FreedomCaucus <- RepAttributes$FreedomCaucus
V(CA.net)$WaterCaucus <- RepAttributes$WaterCaucus
V(CA.net)$EnergyClimateCaucus <- RepAttributes$EnergyClimateCaucus
V(CA.net)$ITCaucus <- RepAttributes$ITCaucus
V(CA.net)$ChildCaucus <- RepAttributes$ChildCaucus
V(CA.net)$TermSeniority <- RepAttributes$TermSeniority

###############
## Figure 4 Plot of Coauthor and Joint author networks
###############

#Coauthor Network 
set.seed(1)
l <- layout.fruchterman.reingold(CA.net)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)

plot(CA.net,
     vertex.label = NA,
     vertex.color=c("red", "blue")[1+V(CA.net)$Democrat],
     vertex.size = 7,
     edge.width = 0.1, 
     edge.arrow.size = 0.1,
     edge.arrow.width = 0.5,
     frame = TRUE,
     rescale = F,
     layout = l)

#Joint author Network
set.seed(1)
l <- layout.fruchterman.reingold(JA.net)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)

plot(JA.net,
     vertex.label = NA,
     vertex.color=c("red", "blue")[1+V(JA.net)$Democrat],
     vertex.size = 7,
     edge.width = 0.1, 
     edge.arrow.size = 0.1,
     edge.arrow.width = 0.5,
     frame = TRUE,
     rescale = F,
     layout = l)


###############
## Figure 5 E-I Index scores for coauthorship and joint authorship
###############
# install homophily package from github
library(devtools)
install_github("knapply/homophily")

#individual for party by the democrat variable
JApartyEI <- homophily::ei_index(JA.net, node_attr_name = "Democrat", scope = "node")

JAoutdeg <- degree(JA.net, mode = "out")

# new aug 8 addition
JA.EI.outdeg <- cbind(RepAttributes$RepName, JApartyEI, JAoutdeg)
colnames(JA.EI.outdeg) <- c("RepName", "partyEI", "outdegree")


CA.EIout <- as.data.frame(matrix(NA,148,1))

for(i in 1:148){
  #get the original coauthor network every time.
  temp <- CoauthorNetwork
  #zero out the ith representative to only calculate the outward facing ties in the E-I index.
  temp[,i] <- matrix(0,148,1)
  #re-initialize the network
  temp.net <- graph_from_adjacency_matrix(temp,
                                          mode= c("directed"),
                                          weighted = NULL, 
                                          diag = FALSE, add.colnames = F) 
  #put in the attributes (short list)
  V(temp.net)$RepName <- RepAttributes$RepName
  V(temp.net)$Democrat <- RepAttributes$Democrat
  #calculate the outward ties only E-I index for the ith representative.
  CA.EIout[i,1] <- homophily::ei_index(temp.net, node_attr_name = "Democrat", scope = "node")[i]
}

#joint author network first
mean(JApartyEI)
sd(JApartyEI)
summary(JApartyEI)

#coauthor network
mean(CA.EIout[,1])
sd(CA.EIout[,1])
summary(CA.EIout[,1])

#this is the CApartyEI variable from calculating EI for coauthorship ties of both directions. 
CApartyEI <- homophily::ei_index(CA.net, node_attr_name = "Democrat", scope = "node") #this is the two way coauthor ei index 

EIindexes <- as.data.frame(cbind(RepAttributes$RepName, as.numeric(JApartyEI), as.numeric(CA.EIout[,1]), as.numeric(CApartyEI), RepAttributes$Democrat))
colnames(EIindexes) <- c("RepName", "JApartyEI", "CApartyEIout", "CApartyEI", "Party (Dem = 1)")

library(ggplot2)
ggplot(EIindexes, aes(x = as.numeric(JApartyEI), y = as.numeric(CApartyEIout), label = RepName, color = `Party (Dem = 1)`)) + 
  geom_point() +
  geom_text(vjust = 1, color = "black", size = 2) + 
  scale_color_manual(values = c("red", "blue")) +
  geom_hline(yintercept = 0, color = "black", linetype = "solid") + 
  geom_vline(xintercept = 0, color = "black", linetype = "solid") + 
  xlim(c(-1,1)) + 
  ylim(c(-1,1)) + 
  labs(title="Joint and Coauthor EI", y="Coauthor EI", x="Jointauthor EI")


############
## Figure 6 Neighbor networks illustrating coauthorship linkages
###########
## Figure 6A 
who <- 89  

#coauthor network
egonet.ca <- induced_subgraph(CA.net, c(who, neighbors(CA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ca)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ca,
     vertex.label = V(egonet.ca)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ca)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

## Figure 6B 
who <- 21  

#coauthor network
egonet.ca <- induced_subgraph(CA.net, c(who, neighbors(CA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ca)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ca,
     vertex.label = V(egonet.ca)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ca)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

## Figure 6E in Figure 6
who <- 19  

#coauthor network
egonet.ca <- induced_subgraph(CA.net, c(who, neighbors(CA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ca)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ca,
     vertex.label = V(egonet.ca)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ca)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

## Figure 6F in Figure 6
who <- 66 

#coauthor network
egonet.ca <- induced_subgraph(CA.net, c(who, neighbors(CA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ca)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ca,
     vertex.label = V(egonet.ca)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ca)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)


## Figure 6G in Figure 6
who <- 31 

#coauthor network
egonet.ca <- induced_subgraph(CA.net, c(who, neighbors(CA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ca)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ca,
     vertex.label = V(egonet.ca)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ca)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

## Figure 6H in Figure 6
who <- 35 

#coauthor network
egonet.ca <- induced_subgraph(CA.net, c(who, neighbors(CA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ca)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ca,
     vertex.label = V(egonet.ca)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ca)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)


## Figure 6C in Figure 6
who <- 26  

#coauthor network
egonet.ca <- induced_subgraph(CA.net, c(who, neighbors(CA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ca)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ca,
     vertex.label = V(egonet.ca)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ca)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

## Figure 6D in Figure 6
who <- 70 

#coauthor network
egonet.ca <- induced_subgraph(CA.net, c(who, neighbors(CA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ca)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ca,
     vertex.label = V(egonet.ca)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ca)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

#####
# #Figure 7 Neighbor networks illustrating joint authorship linkages
#####

## Figure 7A
#joint author network
who <- 89 
egonet.ja <- induced_subgraph(JA.net, c(who, neighbors(JA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ja)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ja,
     vertex.label = V(egonet.ja)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ja)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

#Figure 7B
#joint author network
who <- 21 
egonet.ja <- induced_subgraph(JA.net, c(who, neighbors(JA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ja)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ja,
     vertex.label = V(egonet.ja)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ja)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

#Figure 7C
#joint author network
who <- 31 
egonet.ja <- induced_subgraph(JA.net, c(who, neighbors(JA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ja)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ja,
     vertex.label = V(egonet.ja)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ja)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)

#Figure 7D
#joint author network
who <- 35 
egonet.ja <- induced_subgraph(JA.net, c(who, neighbors(JA.net, who, mode = "out")))

set.seed(1)
l <- layout_with_fr(egonet.ja)
l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -2.4, xmax = 2.4)                              
plot(egonet.ja,
     vertex.label = V(egonet.ja)$RepName,
     vertex.label.color = "black",
     vertex.label.dist = 1,
     vertex.label.degree = -pi/2,
     vertex.label.cex = 0.75,
     vertex.color=c("red", "blue")[1+V(egonet.ja)$Democrat],
     vertex.size = 8,
     edge.width = 0.2, 
     edge.arrow.size = 0.5,
     edge.arrow.width = 0.5,
     rescale = F,
     layout = l)


###############
## Figure 9 Histograms of Joint and Co-author outdegree.
###############
JAoutdegree <- degree(JA.net, mode = "out")
hist(JAoutdegree)

CAoutdegree <- degree(CA.net, mode = "out")
hist(CAoutdegree)


##############
## Table 4 Effects of Representatives' attributes on their E-I Index
##############


#adding the EI indexes as new variables
RepAttributes$CApartyEIout <- CA.EIout$V1 #add the Coauthor EI index for outward ties only
#RepAttributes$CApartyEI <- CApartyEI #add the Coauthor EI index for two way ties
RepAttributes$JApartyEI <- JApartyEI

#run model with outcome as Coauthor EI (one way) (this is Table 4 model 1)
tab4a <- lm(CApartyEIout ~ Hispanic + Black + Asian + Woman +
                        WaterCaucus +  EnergyClimateCaucus + ITCaucus + ChildCaucus + FreedomCaucus + 
                        TermSeniority + ComChair, data = RepAttributes)

#run model with outcome as Joint Author EI
tab4b <- lm(JApartyEI ~ Hispanic + Black + Asian + Woman +
                        WaterCaucus +  EnergyClimateCaucus + ITCaucus + ChildCaucus + FreedomCaucus + 
                        TermSeniority + ComChair + TotalBillsAuthored, data = RepAttributes)

library(modelsummary)
#check model 
modelsummary(
  list("Coauthor E-I Index (out only)" = tab4a, "Joint Author E-I Index" = tab4b), 
  coef_rename = c("(Intercept)" = "Intercept"),
  gof_omit = c('Log.Lik|IC|RMSE'), 
  stars = TRUE, 
 )

# F-statistic for model 1 in Table 4
summary(tab4a)$fstatistic
#F: 8.338604 on 11 and 136 df. 
pf(8.338604, 11, 136, lower.tail = F)
#p-value: 4.438875e-11 ***

# F-statistic for model 2 in Table 4
summary(tab4b)$fstatistic
#F: 2.778882 on 12 and 135 df. 
pf(2.778882, 12, 135, lower.tail = F)
#p-value: 0.002057952 **


#save model outputs as tex file 
modelsummary(
  list("Coauthor E-I Index (out only)" = tab4a, "Joint Author E-I Index" = tab4b), 
  coef_rename = c("(Intercept)" = "Intercept"),
  gof_omit = c('Log.Lik|IC|RMSE'), 
  stars = TRUE, 
  output = "table4.tex"
)

#write.csv(RepAttributes, file = "RepAttributes_Aug11END.csv")





